package com.xhsj.user.pms.patent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xhsj.user.pms.base.BaseController;
import com.xhsj.user.pms.patent.domain.PatentFile;
import com.xhsj.user.pms.patent.service.PatentFileService;
import com.xhsj.user.utils.Message;
import com.xhsj.user.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Auther: 罗季嘉
 * @Date: 2020/1/17 16:24
 */
@Slf4j
@Api(value = "文件相关" ,description = "文件相关")
@RequestMapping("/patentFile")
@Controller
@CrossOrigin(origins = "*", maxAge = 3600)
public class PatentFileController extends BaseController {

    @Autowired
    private PatentFileService patentFileService;

    /**
     * 获取该专利申请文件
     * @param
     * @return
     */
    @PostMapping("/getApplicationFileByDisclosureId/v1")
    @ApiOperation(value="得到专利申请文件",notes ="得到专利申请文件" )
    @ApiImplicitParam(name = "param", value = "{\n" +
            "\"pageNum\":当前页,\n" +
            "\"pageSize\":有几条数据,\n" +
            "\"disclosureId\":\"交底书ID/专利ID\"\n" +
            "}", required = true, dataType = "Map")
    @ResponseBody
    public Message getApplicationFileByDisclosureId(@RequestBody Map<String,Object> param){
        try {
        log.info("专利申请文件参数，{}",param);
        if(StringUtils.isNull(param.get("disclosureId")))
            return Message.error(1,"disclosureId参数不能为空");
          ;
        IPage<PatentFile> patentFiles = patentFileService.selectApplicationFileByDisclosureId(param);
        return Message.success("成功",patentFiles.getRecords(),patentFiles.getTotal(),patentFiles.getPages());
        }catch (Exception e){
            log.info("专利申请文件错误信息，{}",e.getMessage());
            return Message.error(1,"失败");
        }
    }

    /**
     * 获取该专利申请文件
     * @param
     * @return
     */
    @PostMapping("/searchApplicationFileByFileName/v1")
    @ApiOperation(value="根据名称搜索获取专利申请文件",notes ="根据名称搜索获取专利申请文件" )
    @ApiImplicitParam(name = "param", value = "{\n" +
            "\"pageNum\":当前页,\n" +
            "\"pageSize\":有几条数据,\n" +
            "\"disclosureId\":\"交底书ID/专利ID\",\n" +
            "\"fileName\":\"搜索名\"\n" +
            "}" +
            "}", required = true, dataType = "Map")
    @ResponseBody
    public Message searchApplicationFileByFileName(@RequestBody Map<String,Object> param){
        try {
            log.info("根据名称搜索获取专利申请文件参数，{}",param);
            if(StringUtils.isNull(param.get("disclosureId")))
                return Message.error(1,"disclosureId参数不能为空");
            int pageNum=1;
            int pageSize=20;
            if(StringUtils.isNotNull(param.get("pageNum")))
                pageNum = Integer.parseInt(String.valueOf(param.get("pageNum")));
            if(StringUtils.isNotNull(param.get("pageSize")))
                pageSize = Integer.parseInt(String.valueOf(param.get("pageSize")));
            String disclosureId = String.valueOf(param.get("disclosureId"));
            String fileName = String.valueOf(param.get("fileName"));
            List<Integer> fileTypeList = new ArrayList<>();
            fileTypeList.add(1);
            fileTypeList.add(3);
            IPage<PatentFile> patentFiles = patentFileService.searchApplicationFileByFileName(disclosureId,fileName,fileTypeList,pageNum,pageSize);
            return Message.success("成功",patentFiles.getRecords(),patentFiles.getTotal(),patentFiles.getPages());
        }catch (Exception e){
            log.error("错误信息",e);
            return Message.error(1,"失败");
        }
    }



    @PostMapping("/getNoticeFileByDisclosureId/v1")
    @ApiOperation(value="通知相关文件列表",notes ="通知相关文件列表" )
    @ApiImplicitParam(name = "param", value = "{\n" +
            "\"pageNum\":当前页,\n" +
            "\"pageSize\":有几条数据,\n" +
            "\"disclosureId\":\"交底书ID/专利ID\"\n" +
            "}", required = true, dataType = "Map")
    @ResponseBody
    public Message getNoticeFileByDisclosureId(@RequestBody Map<String,Object> param){
        try {
            log.info("通知相关文件列表，{}",param);
            if(StringUtils.isNull(param.get("disclosureId")))
                return Message.error(1,"disclosureId参数不能为空");

            IPage<Map<String, Object>> patentFiles = patentFileService.getNoticeFileByDisclosureId(param);
            return Message.success("成功",patentFiles.getRecords(),patentFiles.getTotal());
        }catch (Exception e){
            log.info("通知相关文件列表错误信息，{}",e.getMessage());
            return Message.error(1,"失败");
        }
    }


    @PostMapping("/searchNoticeFileByFileName/v1")
    @ApiOperation(value="根据文件名或日期获得通知相关文件列表",notes ="根据文件名或日期获得通知相关文件列表" )
    @ApiImplicitParam(name = "param", value = "{ \n" +
            "\"pageNum\":1, \n" +
            "\"pageSize\":20, \n" +
            "\"disclosureId\":\"B1F349EB1A654126AFA939E1A0DEC5B1\",\n" +
            "\"fileName\":\"\",\n" +
            "\"uploadTime\":\n" +
            "} ", required = true, dataType = "Map")
    @ResponseBody
    public Message searchNoticeFileByFileName(@RequestBody Map<String,Object> param){
        try {
            log.info("根据文件名或日期获得通知相关文件列表参数，{}",param);
            if(StringUtils.isNull(param.get("disclosureId")))
                return Message.error(1,"disclosureId参数不能为空");

            IPage<Map<String,Object>> patentFiles = patentFileService.searchNoticeFileByFileName(param);
            return Message.success("成功",patentFiles.getRecords(),patentFiles.getTotal());

        }catch (Exception e){
            log.info("错误信息",e);
            return Message.error(1,"失败");
        }
    }

    //更新专利文件表
    @PostMapping("/updateReviewPatentFile/v1")
    @ApiOperation(value="更新专利文件表",notes ="更新专利文件表" )
    @ResponseBody
    public Message updateReviewPatentFile(@RequestBody PatentFile patentFile){
        return   patentFileService.updateReviewPatentFile(patentFile);
    }

}
